BeginScene = {}
BeginSceneFunc = {}
setmetatable(BeginScene, BeginSceneFunc)
BeginSceneFunc.__index = BeginSceneFunc

BeginSceneFunc.getNode = function()
  local l = {}
  setmetatable(l, BeginSceneFunc)
  l.node = CCLayer:create()
  
  l.registerNode = CCBReader:load("LoginScene3.ccbi")
  l.registerNode:setPosition(ccp(-960, 0))
  l.node:addChild(l.registerNode)

  l.beginNode = CCBReader:load("LoginScene1.ccbi")
  l.node:addChild(l.beginNode)

  l.loginNode = CCBReader:load("LoginScene2.ccbi")
  l.loginNode:setPosition(ccp(960, 0))
  l.node:addChild(l.loginNode)
  l.checkBox = CheckBox:create(l.loginNode:getChildByTag(1001))

  l.currentName = "qqqq0001" 
  l.currentPasswd = "123456" 
  BeginScene.instance = l
  l:init()
  l:onLoginButton()
  return l
end

BeginSceneFunc.getScene = function()
  local sc = CCScene:create()
  local bksp = CCSprite:create("login_bk1.png")
  bksp:setPosition(ccp(480, 320))
  sc:addChild(bksp)
  sc:addChild(BeginScene:getNode().node)
  return sc
end

BeginSceneFunc.initLogin = function(self)
  local nameEditBox = CCEditBox:create(CCSizeMake(350, 60), "editbox_bk1.png", "点击输入")
  nameEditBox:setPosition(ccp(410, 425))
  self.loginNode:addChild(nameEditBox)
  setObjectExtTable(nameEditBox, self)
  self.nameEditBox = nameEditBox
  --self.nameEditBox:setText("efttest0003")
  self.nameEditBox:setText(self.currentName)

  local passwdEditBox = CCEditBox:create(CCSizeMake(350, 60), "editbox_bk1.png", "点击输入")
  passwdEditBox:setPosition(ccp(410, 345))
  passwdEditBox:setInputFlag(0)
  self.loginNode:addChild(passwdEditBox)
  setObjectExtTable(passwdEditBox, self)
  self.passwdEditBox = passwdEditBox 
  self.passwdEditBox:setText(self.currentPasswd)
end

BeginSceneFunc.initRegister = function(self)
  local nameEditBox = CCEditBox:create(CCSizeMake(400, 60), "editbox_bk1.png", "点击输入")
  nameEditBox:setPosition(ccp(550, 462))
  self.loginNode:addChild(nameEditBox)
  setObjectExtTable(nameEditBox, self)
  self.nameEditBox = nameEditBox

  local passwdEditBox = CCEditBox:create(CCSizeMake(400, 60), "editbox_bk1.png", "点击输入")
  passwdEditBox:setPosition(ccp(550, 355))
  passwdEditBox:setInputFlag(0)
  self.loginNode:addChild(passwdEditBox)
  setObjectExtTable(passwdEditBox, self)
  self.passwdEditBox = passwdEditBox 

  local passwdEditBox1 = CCEditBox:create(CCSizeMake(400, 60), "editbox_bk1.png", "点击输入")
  passwdEditBox1:setPosition(ccp(550, 244))
  passwdEditBox1:setInputFlag(0)
  self.loginNode:addChild(passwdEditBox1)
  setObjectExtTable(passwdEditBox1, self)
  self.passwdEditBox1 = passwdEditBox1
end



BeginSceneFunc.init = function(self)
  setObjectExtTable(self.beginNode:getChildByTag(1020), self)
  setObjectExtTable(self.beginNode:getChildByTag(1021), self)
  setObjectExtTable(self.beginNode:getChildByTag(1022), self)
  
  setObjectExtTable(self.loginNode:getChildByTag(1020), self)
  setObjectExtTable(self.loginNode:getChildByTag(1021), self)
  
  setObjectExtTable(self.registerNode:getChildByTag(1020), self)
  setObjectExtTable(self.registerNode:getChildByTag(1021), self)
end
BeginSceneFunc.onRegisterButton = function(self, node, event)
  local ma = CCMoveTo:create(0.5, ccp(960, 0))
  self.node:runAction(ActionUtil.createSeqAction({ma, CCCallFuncN:create(BeginSceneFunc.onRegisterActionEnd)}))
end
BeginSceneFunc.onLoginButton = function(self, node, event)
  local ma = CCMoveTo:create(0.5, ccp(-960, 0))
  self.node:runAction(ActionUtil.createSeqAction({ma, CCCallFuncN:create(BeginSceneFunc.onLoginActionEnd)}))
end
BeginSceneFunc.onLoginActionEnd = function(node)
  BeginScene.instance:initLogin()
end
BeginSceneFunc.onRegisterActionEnd = function(node)
  BeginScene.instance:initRegister()
end
BeginSceneFunc.clearEdits = function(self)
  if self.nameEditBox~=nil then
    self.loginNode:removeChild(self.nameEditBox, true)
    self.nameEditBox = nil
  end 
  if self.passwdEditBox~=nil then
    self.loginNode:removeChild(self.passwdEditBox, true) 
    self.passwdEditBox = nil
  end
  if self.passwdEditBox1~=nil then
    self.loginNode:removeChild(self.passwdEditBox1, true) 
    self.passwdEditBox1 = nil
  end
end
BeginSceneFunc.enableEdits = function(self, enable)
  if self.nameEditBox~=nil then
    TypeCast(self.nameEditBox, "CCButton"):setEnabled(enable)
  end 
  if self.passwdEditBox~=nil then
    TypeCast(self.passwdEditBox, "CCButton"):setEnabled(enable)
  end
  if self.passwdEditBox1~=nil then
    TypeCast(self.passwdEditBox1, "CCButton"):setEnabled(enable)
  end
end
BeginSceneFunc.onBackButton = function(self, node, event)
  self:clearEdits() 
  local ma = CCMoveTo:create(0.5, ccp(0, 0))
  self.node:runAction(ma)
end
BeginSceneFunc.onBeginButton = function(self, node, event)
end

------
--register
BeginSceneFunc.onEditBoxReturn = function(self, node, event)
  print("BeginSceneFunc.onEditBoxReturn")
  local x,y = self.node:getPosition()
  if node==self.nameEditBox then
    if x==-960 then return end
    local name = self.nameEditBox:getText()
    local suc = checkUserAccount(name)
    if suc==true then
      NetworkRequest.uniqueCheck(name, "onUniqueCheckComplete", self)
    else
      local lb = TypeCast(self.registerNode:getChildByTag(1010), "CCLabelTTF")
      lb:setString("非法字符")
      lb:setColor(ccc3(255,0,0))
    end
  elseif node==self.passwdEditBox then
    if x==-960 then return end
    local passwd = self.passwdEditBox:getText()
    local lb = TypeCast(self.registerNode:getChildByTag(1011), "CCLabelTTF")
    if checkPasswd(passwd)==true and string.len(passwd)>=6 and string.len(passwd)<=20  then
      lb:setString("密码可用")
      lb:setColor(ccc3(0,255,0))
    else
      lb:setString("密码不符合要求")
      lb:setColor(ccc3(255,0,0))
    end  
  elseif node==self.passwdEditBox1 then
    local tmp = self.passwdEditBox:getText()
    local tmp1 = self.passwdEditBox1:getText()
    local lb = TypeCast(self.registerNode:getChildByTag(1012), "CCLabelTTF")
    if tmp1~=tmp then
      lb:setString("两次输入密码不一致")
      lb:setColor(ccc3(255,0,0))
    else
      self.passwdCorrect = true
      lb:setString("密码一致")
      lb:setColor(ccc3(0,255,0))
    end
  end
end
BeginSceneFunc.onUniqueCheckComplete = function(self, data)
  print("BeginSceneFunc.onUniqueCheckComplete", data)
  local res = cjson.decode(data)
  local lb = TypeCast(self.registerNode:getChildByTag(1010), "CCLabelTTF")
  lb:setString(res.msg)
  if res.ret==0 then
    lb:setColor(ccc3(0,255,0))
    self.nameCorrect = true
  else
    lb:setColor(ccc3(255,0,0))
  end
end
------
----------
--Login
BeginSceneFunc.onLoginConfirmButton = function(self, node, event)
  local name = self.nameEditBox:getText()
  local passwd = self.passwdEditBox:getText()
  print("BeginSceneFunc.onLoginConfirmButton", name, passwd)
  NetworkRequest.login(name, passwd, "onLoginComplete", self)
  LoadScene:showLoadingScene()
  --setTouchEnabledAll(self.node, false) 
  --self:enableEdits(false)
  --self:clearEdits()
end
BeginSceneFunc.onLoginComplete = function(self, data)
  print("BeginSceneFunc.onLoginComplete", data)
  local res = cjson.decode(data)
  if res.ret==0 then
    cayuan_session_key = res["cayuan_session_key"]
    cayuan_user = res["cayuan_user"]  
    --urlTpl = string.format("http://cayuan.war.blessjoy.com/%%s?check=%%s&cayuan_user=%s&cayuan_session_key=%s", cayuan_user, cayuan_session_key)
    urlTpl = string.format("http://cayuan.war.blessjoy.com/%%s?check=%%s&cayuan_user=%s&cayuan_session_key=%s", cayuan_user, cayuan_session_key)
    --urlTpl = string.format("http://war.chaogu001.com/%%s?check=%%s&cayuan_user=%s&cayuan_session_key=%s", cayuan_user, cayuan_session_key)
    print("BeginSceneFunc.onLoginComplete", urlTpl)
    --CCDirector:sharedDirector():replaceScene(SelectNameScene.getScene())
    self.node:getParent():removeChild(self.node, true)
    NetworkRequest.getUserInfo("onGetUserInfoComplete", self)
    self.loadTimerID = CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(BeginSceneFunc.onLoadTimer, 0.01, false)
    self.loadState = 1
  else
    local lb = TypeCast(self.loginNode:getChildByTag(1010), "CCLabelTTF")
    lb:setString("账户或密码不对")
    setTouchEnabledAll(self.node, true) 
    --self:enableEdits(true)
  end
  LoadScene:removeLoadingScene()
end
BeginSceneFunc.onLoadTimer = function()
  local self = BeginScene.instance
  if self.loadState==1 then
    self:showProgress(10)
  elseif self.loadState==2 then
    self:showProgress(30)
    self.userinfoJson  = cjson.decode(self.userinfo)
    --[[
    if nameFromPath(self.userinfoJson.host.headpic)=="000_000_0_lanbo_50.png" then
      CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(BeginScene.instance.loadTimerID)
      print("BeginSceneFunc.onLoadTimer", BeginScene.instance.node)
      self.selectNode = SelectNameScene:getNode()
      CCDirector:sharedDirector():getRunningScene():addChild(self.selectNode.node)
    end]]
    self.loadState = 3
  elseif self.loadState==3 then
    self:showProgress(50)
    if self.userinfoJson.ret==0 then
      UserData.userinfo = self.userinfoJson 
      Player.reload()
      MainScene.loadMainScene()
    else
      print("onGetUserInfo", self.userinfoJson.ret)
    end
    TimeManager.deltaTime = self.userinfoJson.serverTime - os.time()
    CCDirector:sharedDirector():getScheduler():unscheduleScriptEntry(self.loadTimerID)
  end 
end

BeginSceneFunc.onGetUserInfoComplete= function(self, data)
  print("BeginSceneFunc.onGetUserInfoComplete", data)
  self.loadState = 2
  self.userinfo = data
  --[[
  self:showProgress(20)
  local res = cjson.decode(data)
  self:showProgress(40)
  if res==nil then return;end
  if res.ret==0 then
    UserData.userinfo = res 
    Player.reload()
    local msc = MainScene.getScene()
    self:showProgress(100)
    CCDirector:sharedDirector():replaceScene(msc)
    --CCDirector:sharedDirector():replaceScene(SelectNameScene.getScene())
  else
    print("onGetUserInfo", res.ret)
  end
  TimeManager.deltaTime = res.serverTime - os.time()
  --]]
end
BeginSceneFunc.showProgress = function(self, progress)
  if self.progressBar==nil then
    --self.progressBK = CCSprite:create("open_progressbk.png")
    local sc = CCDirector:sharedDirector():getRunningScene()

    local xpbarbk = CCSprite:create("open_progressbk.png")
    xpbarbk:setPosition(480,100)
    sc:addChild(xpbarbk)

    local xpsp = CCSprite:create("open_progress.png")
    local xpbar = CCProgressTimer:create(xpsp)
    xpbar:setType(kCCProgressTimerTypeBar)
    xpbar:setMidpoint(ccp(0,0))
    xpbar:setBarChangeRate(ccp(1, 0))
    xpbar:setPosition(480,100)
    sc:addChild(xpbar)

    self.progressBar = xpbar
  end
  --self.progressBK = CCSprite:create("open_progressbk.png")
  self.progressBar:setPercentage(progress)
end

----------
BeginSceneFunc.onRegisterConfirmButton = function(self, node, event)
  local name = self.nameEditBox:getText()
  local passwd = self.passwdEditBox:getText() 
  if self.nameCorrect==true and self.passwdCorrect==true then
    self.currentName = name
    self.currentPasswd = passwd 
    NetworkRequest.register(name, passwd, "onRegisterComplete", self)
    LoadScene:showLoadingScene()
  else
    showMessage("输入有误，请重新输入", ccc3(255,255,0))
  end
end
BeginSceneFunc.onRegisterComplete = function(self, data)
  print("BeginSceneFunc.onRegisterComplete", data)
  LoadScene:removeLoadingScene()
  local res = cjson.decode(data)
  if res.ret==0 then
    --self.onLoginButton()
    self:clearEdits()
    local ma = CCMoveTo:create(0.5, ccp(-960, 0))
    self.node:runAction(ActionUtil.createSeqAction({ma, CCCallFuncN:create(BeginSceneFunc.onLoginActionEnd)}))
  else
    self.currentName = "" 
    self.currentPasswd = ""
    showMessage(res.msg, ccc3(255,255,0))
  end 
end

